package com.itheima.web.feedback;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.domain.feedback.Feedback;
import com.itheima.domain.feedback.FeedbackExample;
import com.itheima.service.FeedbackService;
import com.itheima.utils.MailUtil;
import com.itheima.utils.TimeUtil;
import com.itheima.web.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/feedback")
public class FeedbackController extends BaseController {
    @Reference
    private FeedbackService feedbackService;

    @RequestMapping(value = "/toFeedback", name = "跳转用户反馈页面")
    public String toFeedback() {
        return "/feedback/feedback";
    }

    @RequestMapping(value = "/save", name = "保存用户反馈")
    public String save(Feedback feedback) {
        //设置用户反馈id
        feedback.setId(UUID.randomUUID().toString());
        //提出反馈的时间
        feedback.setPutTime(new Date());
        //创建日期
        feedback.setCreateDate(new Date());
        //保存公司id
        feedback.setcId(getCompanyId());
        feedback.setCompanyId(getCompanyId());
        //保存反馈者公司名称
        feedback.setCompanyName(getCompanyName());
        //反馈者部门
        feedback.setCreateDept(getUser().getDeptName());
        //反馈者部门id
        feedback.setDeptId(getUser().getDeptId());
        //反馈用户id
        feedback.setuId(getUser().getId());
        //反馈用户的等级
        feedback.setGegree(Integer.valueOf(getUser().getGender()));
        //设置留言未读,1已读,0未读
        feedback.setNoRead("0");
        //调用service保存
        feedbackService.save(feedback);
        session.setAttribute("feedback", feedback);
        return "redirect:/home/main.do";

    }

    @RequestMapping(value = "/findAll", name = "查询所有未读消息")
    public void findAll() {
        //距今时间保存到数据库
        //查询所有反馈,条件查询
        FeedbackExample feedbackExample = new FeedbackExample();
        FeedbackExample.Criteria criteria = feedbackExample.createCriteria();
        //未读消息
        criteria.andNoReadEqualTo("0");
        //获取用户等级,
        Integer degree = getUser().getDegree();
        if (degree == 4) {  //普通员工
            criteria.andUIdEqualTo(getUser().getId());
        }
         else if (degree == 2) { //企业管理员
            criteria.andDeptIdLike(getUser().getDeptId() + "%");
        }
        List<Feedback> feedbackList1 = feedbackService.findAll(feedbackExample);
        for (Feedback feedback : feedbackList1) {
            //反馈的时间
            Date putTime = feedback.getPutTime();
            //当前时间
            Date date = new Date();
            String timeUtil = TimeUtil.getTimeUtil(date, putTime);
            feedback.setTime(timeUtil);
            //存到数据库中
            feedbackService.update(feedback);
        }
        List<Feedback> feedbackList = feedbackService.findAll(feedbackExample);
        session.setAttribute("feedbackList", feedbackList);
        //未读发给收件人的消息,noRead=0的
        int unread_message = 0;
        for (Feedback feedback : feedbackList) {
            //判断是否为收件人
            if (feedback.getRecipientEmail().equals(getUser().getEmail())){
                unread_message+=1;
            }
        }
        session.setAttribute("unread_message", unread_message);
    }

    @RequestMapping(value = "/reply", name = "跳转反馈查看页面或者收件人回复页面")
    public String reply(String id, String recipientEmail) {
        System.out.println(recipientEmail);
        System.out.println(id);
        //判断邮箱是否为null,  //不是反馈收件人,只能查看反馈
        if (recipientEmail == null || !recipientEmail.equals(getUser().getEmail())) {
            //根据反馈者id查询信息
            Feedback feedback = feedbackService.findById(id);
            //转发到页面
            request.setAttribute("feedback", feedback);
            return "/feedback/feedback_admin";
        }
        //判断反馈收件人是否是登陆用户
        else {
            //根据反馈者id查询信息
            Feedback feedback = feedbackService.findById(id);
            //转发到页面
            request.setAttribute("feedback", feedback);
            return "/feedback/feedback_reply";
        }
    }

    @RequestMapping(value = "/toUpdate", name = "保存反馈回复")
    public String toUpdate(Feedback feedback) {
        //设置解决问题的时间
        feedback.setSolveTime(new Date());
        //设置留言已读,1已读,0未读
        feedback.setNoRead("1");
        //调用service保存
        feedbackService.update(feedback);
        return "redirect:/home/main.do";

    }

    @RequestMapping(value = "/replyMessage", name = "反馈回复消息查询")
    public String replyMessage() {
        //查询所有反馈,条件查询
        FeedbackExample feedbackExampleReply = new FeedbackExample();
        FeedbackExample.Criteria criteria = feedbackExampleReply.createCriteria();
          //反馈的用户已读回复消息
            criteria.andNoReadEqualTo("1");
            criteria.andUIdEqualTo(getUser().getId());
        List<Feedback> feedbackReplyList = feedbackService.findAll(feedbackExampleReply);
        session.setAttribute("feedbackReplyList", feedbackReplyList);
        //已回复的消息,noRead=1的
        session.setAttribute("reply_message", feedbackReplyList.size());
        return "/feedback/feedback_reply";
    }

    @RequestMapping(value = "/feedbackLook", name = "查看回复的消息页面")
    public String feedbackLook(String id) {
        Feedback feedbackServiceById = feedbackService.findById(id);
        request.setAttribute("feedbackServiceById", feedbackServiceById);
        if (!feedbackServiceById.getRecipientEmail().equals(getUser().getEmail())) {
            //回复已读不显示
            Feedback feedback = new Feedback();
            feedback.setId(id);
            //回复已读了
            feedback.setNoRead("3");
            //更新
            feedbackService.update(feedback);
        }
        return "/feedback/feedback_reply_look";
    }
}
